#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8

#系统主目录
setup_path="/hcdemo"

if [ "$1" ];then
	IDC_CODE=$1
fi
Red_Error(){
  echo '=================================================';
  printf '\033[1;31;40m%b\033[0m\n' "$1";
  exit 0;
}
#系统判断是不是64位
is64bit=$(getconf LONG_BIT)
if [ "${is64bit}" != '64' ];then
	Red_Error "抱歉, 小区服务相关不支持32位系统, 请使用64位系统!";
fi
Install_Check(){
	while [ "$yes" != 'yes' ] && [ "$yes" != 'n' ]
	do
		echo -e "----------------------------------------------------"
		echo -e "已有Web环境，安装物业服务端可能影响现有站点            "
		echo -e "----------------------------------------------------"
		read -p "输入yes强制安装/Enter yes to force installation (yes/n): " yes;
	done
	if [ "$yes" == 'n' ];then
		exit;
	fi	 
}
System_Check(){
	for serviceS in nginx httpd mysqld dockerd redis apache
	do
		if [ -f "/etc/init.d/${serviceS}" ]; then
			if [ "${serviceS}" = "httpd" ]; then
				serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/apache)
			elif [ "${serviceS}" = "mysqld" ]; then
				serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/mysql)
			else
				serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/${serviceS})
			else
				serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/dockerd)
			else
			  serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/redis)
			else
			  serviceCheck=$(cat /etc/init.d/${serviceS}|grep /$setup_path/apache)
			fi
			[ -z "${serviceCheck}" ] && Install_Check
		fi
	done
}
#检查yum包
Get_Pack_Manager(){
	if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
		PM="yum"
	elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
		PM="apt-get"
	fi
}
#核心下载速度优先,连接母站地址和下载区域
get_Download_url(){
	if [ ! -f /bin/curl ];then
		if [ "${PM}" = "yum" ]; then
			yum install curl -y
		elif [ "${PM}" = "apt-get" ]; then
			apt-get install curl -y
		fi
	fi

	echo '---------------------------------------------';
	echo "Selected download node...";
	nodes=(https://dl.winqi.cn https://gitee.com/java110);
	i=1;
	for node in ${nodes[@]};
	do
		start=`date +%s.%N`
		result=`curl -sS --connect-timeout 3 -m 60 $node/check.txt`
		if [ $result = 'True' ];then
			end=`date +%s.%N`
			start_s=`echo $start | cut -d '.' -f 1`
			start_ns=`echo $start | cut -d '.' -f 2`
			end_s=`echo $end | cut -d '.' -f 1`
			end_ns=`echo $end | cut -d '.' -f 2`
			time_micro=$(( (10#$end_s-10#$start_s)*1000000 + (10#$end_ns/1000 - 10#$start_ns/1000) ))
			time_ms=$(($time_micro/1000))
			values[$i]=$time_ms;
			urls[$time_ms]=$node
			i=$(($i+1))
			if [ $time_ms -lt 100 ];then
				break;
			fi
		fi
	done
	j=5000
	for n in ${values[@]};
	do
		if [ $j -gt $n ];then
			j=$n
		fi
		if [ $j -lt 100 ];then
			break;
		fi
	done
	if [ $j = 5000 ];then
		NODE_URL='https://dl.winqi.cn';
	else
		NODE_URL=${urls[$j]}
	fi
	download_Url=$NODE_URL
	echo "优先选择下载地址: $download_Url";
	echo '---------------------------------------------';
}
Install_RPM_Pack(){
	yumPath=/etc/yum.conf
	Centos8Check=$(cat /etc/redhat-release | grep ' 8.' | grep -iE 'centos|Red Hat')
	isExc=$(cat $yumPath|grep httpd)
	if [ "$isExc" = "" ];then
		echo "exclude=httpd nginx php mysql" >> $yumPath
	fi

	yumBaseUrl=$(cat /etc/yum.repos.d/CentOS-Base.repo|grep baseurl=http|cut -d '=' -f 2|cut -d '$' -f 1|head -n 1)
	[ "${yumBaseUrl}" ] && checkYumRepo=$(curl --connect-timeout 5 --head -s -o /dev/null -w %{http_code} ${yumBaseUrl})
	if [ "${checkYumRepo}" != "200" ];then
		curl -Ss --connect-timeout 3 -m 60 https://dl.winqi.cn/sh/yumRepo_select.sh|bash
	fi

	#尝试同步系统时间
	echo '正在同步系统时间...'
	if [ -z "${Centos8Check}" ]; then
		yum install ntp -y
		rm -rf /etc/localtime
		ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

		#尝试同步国际时间(从阿里云ntp服务器)
		ntpdate ntp1.aliyun.com
		setenforce 0
	fi

	startTime=`date +%s`

	sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
	#yum remove -y python-requests python3-requests python-greenlet python3-greenlet
	yumPacks="wget  tar zip unzip openssl openssl-devel gcc libxml2 libxml2-devel libxslt* zlib zlib-devel libjpeg-devel libpng-devel libwebp libwebp-devel freetype freetype-devel lsof pcre pcre-devel vixie-cron crontabs icu libicu-devel c-ares"
	yum install -y ${yumPacks}

	for yumPack in ${yumPacks}
	do
		rpmPack=$(rpm -q ${yumPack})
		packCheck=$(echo ${rpmPack}|grep not)
		if [ "${packCheck}" ]; then
			yum install ${yumPack} -y
		fi
	done
	if [ -f "/usr/bin/dnf" ]; then
		dnf install -y redhat-rpm-config
	fi

	yum install epel-release -y

}
#创建目录配置文件
Setup_Count (){
	if [! -f /var/hc_setupPath.conf ];then
	echo /hcdemo >/var/hc_setupPath.conf
	if
}

#这是最新的